<?php

namespace app\admin\controller\ykjp\information\basisinfo;

use app\common\controller\Backend;
use think\Db;

/**
 * 客户信息
 *
 * @icon fa fa-circle-o
 */
class Customerinfo extends Backend {

    /**
     * Customerinfo模型对象
     * @var \app\admin\model\ykjp\information\basisinfo\Customerinfo
     */
    protected $model = null;
    protected $distinguish = true;
    protected $dataLimit = false;
    protected $dataLimitField = 'salesman_id';

    public function _initialize() {
        parent::_initialize();
        $authGoup=$this->auth->getGroupIds();
        $yjconfig=get_addon_config("ykjp");
        $this->view->assign("adminlimit",false);
        if(in_array(1,$authGoup)||$yjconfig['cinfo']==0){
            $this->dataLimit=false;  
        }else{
           if($yjconfig['cinfo']==1){
                $this->dataLimit="auth";
           }else{
               $this->dataLimit="personal";
           }
           $this->view->assign("adminlimit",true);  
        }
        $this->model = new \app\admin\model\ykjp\information\basisinfo\Customerinfo;
        $code="CT".date("YmdHis",time());
        $this->view->assign("code",$code);
        $this->view->assign("salesmanIdList", $this->model->getSalesmanIdList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    /**
     * 业务员信息
     */
    public function getsalesman() {
        $this->dataLimit=false;
        $result = array("rows" => [], "total" => 0);
        if ($this->request->isAjax()) {
            $this->model = model("admin");
            return parent::selectpage();
            // if($this->request->request("keyValue")){
            //     $id = $this->request->request("keyValue");
            //     $admin=model("admin");
            //     $list=$admin
            //         ->field("id,username")
            //         ->where("firmid",$this->auth->firmid)
            //         ->where("id",$id)
            //         ->select();
            //     return ['total'=>1, 'list'=>$list];
            // }
            // $admin=model("admin");
            // $list=$admin
            //     ->field("id,username")
            //     ->where("firmid",$this->auth->firmid)
            //     ->select();
            // $count=$admin
            //     ->field("id,username")
            //     ->where("firmid",$this->auth->firmid)
            //     ->count();
            // $result=array("rows"=>$list,"total"=>$count);
            // return json($result);
        }
        return json($result);
    }

    /**
     * @return \think\response\Json
     *
     *
     */
    public function selectpage() {
        return parent::selectpage();
    }

    /**
     * 删除
     */
    public function del($ids = "") {
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            if ($this->distinguish && $this->auth->firmid != 0) {
                $list = $this->model->where($pk, 'in', $ids)->where("firmid", $this->auth->firmid)->select();
            } else {
                $list = $this->model->where($pk, 'in', $ids)->select();
            }
            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    $id = $v["id"];
                    $isSell = Db::name('ykjp_sell')->where('customer_info_id', $v["id"])->find(); //$Order->where("supplier_",$v['id'])->find();
                    $isDelivery = Db::name('ykjp_delivery')->where('customer_info_id', $v["id"])->find(); //$Storage->where("supplier_id",$v['id'])->find();
                    $isSellReturn = Db::name('ykjp_sell_return')->where('customer_info_id', $v["id"])->find(); //$Retire->where("supplier_id",$v['id'])->find();
                    if ($isSell || $isDelivery || $isSellReturn) {
                        $this->model->rollback();
                        $this->error(__("Cant deltete"));
                    }
                    $count += $v->delete();
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

}
